package com.xiaohudie.SGGio;

import lombok.SneakyThrows;

import java.io.*;

/**
 * @Author xiao hu die
 * @Date 2022/3/23 18:41
 * @Version 1.0
 */
public class FileReaderTest {

 public static void main(String[] args) {
 //test1();
 //   test04();
 /* for (int i = 0; i < 5; i++) {
   test05();
  }*/
  test06();
 }
 @SneakyThrows
 public static void test1(){
  /**指定文件的具体位置*/

  File file =new File("D:\\java图片代码\\11.txt");
  /**提供相应的流*/
  FileReader fileReader = new FileReader(file);
  try {
   int read = fileReader.read();
   while (read!=-1){
    System.out.print((char)read);
    read=fileReader.read();
   }}finally {
   /**必须关闭流*/
   fileReader.close();
   System.out.println("流已经关闭了");
  }
 }
 public static void test02(){
  File file = new File("D:\\java图片代码\\11.txt");
  FileReader fileReader = null;
  try {
   fileReader = new FileReader(file);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  try {
   int read;
   while ((read =fileReader.read())!=-1){
    System.out.println((char)read);
   }
  }catch (IOException e){
   e.printStackTrace();
  }finally {
   try {
    if (fileReader!=null) {
     fileReader.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 public static void test03(){
  File file = new File("D:\\java图片代码\\11.txt");
  FileReader fileReader =null;
  try {
   fileReader =new FileReader(file);
   char[] chars =new char[10];
   int len;
   while ((len =fileReader.read(chars))!=-1){
    /**得到这些字符数组，然后遍历得到具体的数据 */
    for (int i = 0; i < len; i++) {
     System.out.println(chars[i]);
    }
   }
  }catch (IOException e){
   e.printStackTrace();
  }finally {
   if (fileReader !=null){
    try {
     fileReader.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }
 /**默认的够着构造方法就是写完再写就是覆盖*/
 public static void test04(){
  File file = new File("D:\\\\java图片代码\\\\22.txt");
  FileWriter fileWriter =null ;
  try {
    fileWriter = new FileWriter(file,false);
    fileWriter.write("I have a dream");
    fileWriter.write("You need have a dream too");
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
   try {
    if (fileWriter != null) {
     fileWriter.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 /**默认的够着构造方法就是写完再写就是覆盖*/
 public static void test05(){
  File file = new File("D:\\\\java图片代码\\\\22.txt");
  FileWriter fileWriter =null ;
  try {
   fileWriter = new FileWriter(file,true);
   fileWriter.write("I have a dream\n");
   fileWriter.write("You need have a dream too");
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
   try {
    if (fileWriter!=null) {
     fileWriter.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 public static void test06(){
  /**创建写入与写出的文件类*/
  File file = new File("E:\\11111.txt");
  File file1 = new File("D:\\\\java图片代码\\\\44.txt");
  /**创建输入与输出流*/
  FileReader fileReader = null;
  FileWriter fileWriter =null;
try {
  fileReader =new FileReader(file);
  fileWriter =new FileWriter(file1);
  /**数据的读入与写出操作*/
  char[] chars  =new char[5];
  int len;
  while ((len=fileReader.read(chars)) !=-1){
   fileWriter.write(chars,0,len);
  }}catch (IOException e){
    e.printStackTrace();
}finally {
 if (fileReader!=null){
  try {
   fileReader.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 if (fileWriter!=null){
  try {
   fileWriter.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}
 }
}
